﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.Week;

namespace Benefit.Service.Week
{
    /// <summary>
    /// 公司周累计
    /// </summary>
    public class ServiceCompanyWeekSum : Benefit.Interface.Week.ICompanyWeekSum
    {
        Benefit.DB.DBManager db = null;
        public ServiceCompanyWeekSum(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        public void InitCompanyWeekSum(int year, int week, int month)
        {
            CompanyWeekSum sum = new CompanyWeekSum();
            var query = db.OperatorWeekSum.Where(a => a.Year == year).Where(a => a.Week == week);
            var queryExist = db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Week == week);
            if (queryExist.Count() > 0)
            {
                queryExist.First().AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                queryExist.First().AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                queryExist.First().BillCount = query.Sum(a => a.BillCount);
                queryExist.First().DayCount = query.Sum(a => a.DayCount);
                queryExist.First().Free = query.Sum(a => a.Free);
                queryExist.First().LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                queryExist.First().MaxReturn = query.Sum(a => a.MaxReturn);
                queryExist.First().OverStopLoss = query.Sum(a => a.OverStopLoss);
                queryExist.First().PingJinPing = Convert.ToInt32(query.Sum(a => a.PingJinPing));
                queryExist.First().Profit = query.Sum(a => a.Profit);
                queryExist.First().RaiseRate = query.Sum(a => a.RaiseRate);
                queryExist.First().RiskCount = Convert.ToInt32(query.Sum(a => a.RiskCount));
                queryExist.First().SumLost = query.Sum(a => a.SumLost);
                queryExist.First().SumWin = query.Sum(a => a.SumWin);
                queryExist.First().UsedMargin = query.Sum(a => a.UsedMargin);
                queryExist.First().Utilization = query.Sum(a => a.Utilization);
                queryExist.First().WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                queryExist.First().WinRate = query.Sum(a => a.WinRate);
                queryExist.First().Yield = query.Sum(a => a.Yield);
            }
            else
            {
                sum.AvgBillBenefit = query.Sum(a => a.AvgBillBenefit);
                sum.AvgPostionTime = query.Sum(a => a.AvgPostionTime);
                sum.BillCount = query.Sum(a => a.BillCount);
                sum.DayCount = query.Sum(a => a.DayCount);
                sum.Free = query.Sum(a => a.Free);
                sum.LoseCount = Convert.ToInt32(query.Sum(a => a.LoseCount));
                sum.MaxReturn = query.Sum(a => a.MaxReturn);
                sum.OverStopLoss = query.Sum(a => a.OverStopLoss);
                sum.PingJinPing = Convert.ToInt32(query.Sum(a => a.PingJinPing));
                sum.Profit = query.Sum(a => a.Profit);
                sum.RaiseRate = query.Sum(a => a.RaiseRate);
                sum.RiskCount = Convert.ToInt32(query.Sum(a => a.RiskCount));
                sum.SumLost = query.Sum(a => a.SumLost);
                sum.SumWin = query.Sum(a => a.SumWin);
                sum.UsedMargin = query.Sum(a => a.UsedMargin);
                sum.Utilization = query.Sum(a => a.Utilization);
                sum.WinCount = Convert.ToInt32(query.Sum(a => a.WinCount));
                sum.WinRate = query.Sum(a => a.WinRate);
                sum.Yield = query.Sum(a => a.Yield);
                sum.Year = year;
                sum.Week = week;
                sum.Month = month;

                db.CompanyWeekSum.Add(sum);
            }

            db.SaveChanges();
        }

        public void Delete(int year, int week)
        {
            var query = db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Week == week).ToList();
            foreach (Benefit.Models.Week.CompanyWeekSum avg in query)
            {
                db.CompanyWeekSum.Remove(avg);
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public CompanyWeekSum GetCompanyWeekSum(int year, int week)
        {
            return db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Week == week).ToList().First();
        }

        /// <summary>
        /// 获取交易员查询日期之前的五日均值
        /// </summary>
        /// <param name="operatorId"></param>
        /// <param name="tradeHistoryId"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<CompanyWeekSum> GetCompanyWeekSums(int year, int month)
        {
            return db.CompanyWeekSum.Where(a => a.Year == year).Where(a => a.Month == month).ToList();
        }
    }
}
